
#include <string.h>
#include "card_param.h"

void card_param_clear(card_param_t *param)
{
	/*
	uint8_t i;	
	card_param_block_t *blk;
	
	param->block_amout = 0;

	for(i = 0; i < 8; i++)
	{
		blk = param->param + i;
		blk->present_map = 0;
	}
	*/
	memset(param, 0, sizeof(card_param_t));
}

uint32_t card_param_init(card_param_t *param, uint8_t atr[])
{	
	uint8_t TD;
	uint32_t offset;	
	card_param_block_t *blk;

	card_param_clear(param);
	TD = atr[1] & 0xF0;
	offset = 2;
	while((TD & 0xF0) != 0)
	{
		blk = param->param + param->block_amout;
		param->block_amout++;
		blk->present_map = TD >> 4;
		
		if(is_TA_present(TD))
		{
			blk->data[0] = atr[offset++];
		}

		if(is_TB_present(TD))
		{
			blk->data[1] = atr[offset++];
		}

		if(is_TC_present(TD))
		{
			blk->data[2] = atr[offset++];
		}
		
		if(is_TD_present(TD))
		{
			blk->data[3] = TD = atr[offset++];
		}
		else
		{
			break;
		}
	}
	return offset;
}

uint32_t card_param_block_get_specific_char(card_param_block_t *param_block, uint8_t character)
{
	uint8_t map = param_block->present_map;
	
	if((1 << character) & map)
	{
		return param_block->data[character];
	}
	
	return 0xFFFF;
}


uint32_t card_param_get_specific_char(card_param_t *param, uint8_t character, uint8_t index)
{
	if(index <= param->block_amout)
	{
		card_param_block_t *blk = param->param + index - 1;
		uint8_t map = blk->present_map;
		
		if((1 << character) & map)
		{
			return blk->data[character];
		}
	}
	
	return 0xFFFF;
}

